package org.flint92.springboot.jpa.service;

import org.flint92.springboot.jpa.entity.User;
import org.flint92.springboot.jpa.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * @author flint92
 */
@Service
public class UserService {

    @Resource
    private UserRepository userRepository;

    @PersistenceContext
    private EntityManager entityManager;

    public User findById(int id) {
        return userRepository.findById(id).orElse(null);
    }

    @Transactional
    public void saveUser(User user) {
        entityManager.persist(user);
    }

}
